package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.AddressListResponse
import com.gexiaobao.pigeon.app.model.bean.ProvinceListResponse
import com.gexiaobao.pigeon.app.model.bean.ProvincesAndMunicipalitiesResponse
import com.gexiaobao.pigeon.app.model.bean.ShopCarListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.ShippingAddressParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * @author:hxw
 * @date  :2023/1/9
 * @desc  :
 */
class SettingViewModel : BaseViewModel() {

    var versionName = StringObservableField()

    /** 添加收货地址 */
    val addReceiveAddressName = StringObservableField()
    val addReceiveAddressMobile = StringObservableField()
    val addReceiveAddressArea = StringObservableField()
    val addReceiveAddressDetail = StringObservableField()

    /** 退出登录 */
    val logOutResult: MutableLiveData<UiState> = MutableLiveData()

    /** 注销 */
    val deleteAccountResult: MutableLiveData<UiState> = MutableLiveData()


    /** 退出登录 */
    fun loginOut() {
        request({ apiService.loginOut() }, {
            val deleteState = UiState(isSuccess = true)
            logOutResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            logOutResult.value = deleteState
        })
    }

    /** 注销 */
    fun accountCancellation() {
        request({ apiService.delUser() }, {
            val deleteState = UiState(isSuccess = true)
            deleteAccountResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            deleteAccountResult.value = deleteState
        })
    }

    /** 获取用户设置地址  */
    val addressResult: MutableLiveData<ListDataUiState<AddressListResponse.ListBean>> = MutableLiveData()
    fun getAddressList() {
        request({ apiService.getShippingAddress() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = it.isEmpty(),
                listData = it.list
            )
            addressResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<AddressListResponse.ListBean>()
                )
            addressResult.value = listDataUiState
        })
    }

    /** 编辑收货地址 */
    val editAddressResult: MutableLiveData<UiState> = MutableLiveData()
    fun editShippingAddress(body: ShippingAddressParams) {
        request({ apiService.editShippingAddress(body) }, {
            val deleteState = UiState(isSuccess = true)
            editAddressResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editAddressResult.value = deleteState
        })
    }

    /** 删除收货地址 */
    val delAddressResult: MutableLiveData<UiState> = MutableLiveData()
    fun delShippingAddress(id: String) {
        request({ apiService.delShippingAddress(id) }, {
            val deleteState = UiState(isSuccess = true)
            delAddressResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            delAddressResult.value = deleteState
        })
    }

    /** 获取省份列表 */
    val provinceListResult: MutableLiveData<ProvinceListResponse> = MutableLiveData()
    fun getProvinceList() {
        request({ apiService.getProvinceList() }, {
            provinceListResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取省市区 */
    val provinceAndMunicipalitiesResult: MutableLiveData<ProvincesAndMunicipalitiesResponse> = MutableLiveData()
    fun getProvinceAndMunicipalities() {
        request({ apiService.getProvinceAndMunicipalities() }, {
            provinceAndMunicipalitiesResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取城市列表 */
    val cityListResult: MutableLiveData<ProvinceListResponse> = MutableLiveData()
    fun getCityList(provinceId: Int) {
        request({ apiService.getCityList(provinceId) }, {
            cityListResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取地区列表 */
    val areaListResult: MutableLiveData<ProvinceListResponse> = MutableLiveData()
    fun getAreaList(provinceId: Int, cityId: Int) {
        request({ apiService.getAreaList(provinceId, cityId) }, {
            areaListResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取街道列表 */
    val streetListResult: MutableLiveData<ProvinceListResponse> = MutableLiveData()
    fun getTownList(provinceId: Int, cityId: Int, areaId: Int) {
        request({ apiService.getTownList(provinceId, cityId, areaId) }, {
            streetListResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
}